/*
 *   Copyright (c) 2024 Huawei Device Co., Ltd.
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

// auto-generated {{componentName}}Builder.ets
import { {{componentName}} } from '{{importPath}}'
import { TemplateMgr, TranslateOption, ScaleOption, OpacityOption } from '@hadss/hmrouter'

@Builder
export function {{componentName}}Builder(name: string, param: Object) {
  {{componentName}}Generated()
}

@Component
export struct {{componentName}}Generated {
  @State translateOption: TranslateOption = new TranslateOption()
  @State scaleOption: ScaleOption = new ScaleOption()
  @State opacityOption: OpacityOption = new OpacityOption()
  private pageUrl: string = '{{pageUrl}}'
  private ndId: string = ''

  aboutToAppear(): void {
    TemplateMgr.registerAnimatorHandle(this.pageUrl, this.translateOption, this.scaleOption, this.opacityOption)
    TemplateMgr.registerHMLifecycleHandle(this.pageUrl, this.getUIContext())
  }

  aboutToDisappear(): void {
    TemplateMgr.unRegisterAnimatorHandle(this.ndId)
    TemplateMgr.destroyHMLifecycle(this.pageUrl, this.ndId)
  }

  build() {
    NavDestination() {
      {{componentName}}()
    }
    .mode({{dialog}}?NavDestinationMode.DIALOG:NavDestinationMode.STANDARD)
    .hideTitleBar(true)
    .gesture(PanGesture()
      .onActionStart((event: GestureEvent) => {
        TemplateMgr.interactiveStart(this.ndId, event)
      })
      .onActionUpdate((event: GestureEvent) =>{
        TemplateMgr.interactiveProgress(this.ndId, event)
      })
      .onActionEnd((event: GestureEvent) =>{
        TemplateMgr.interactiveFinish(this.ndId, event)
      })
    )
    .translate(this.translateOption)
    .scale(this.scaleOption)
    .opacity(this.opacityOption.opacity)
    .onAppear(() => {
      TemplateMgr.onAppear(this.pageUrl, this.ndId)
    })
    .onDisAppear(() => {
      TemplateMgr.onDisAppear(this.pageUrl, this.ndId)
    })
    .onShown(() => {
      TemplateMgr.onShown(this.pageUrl, this.ndId)
    })
    .onHidden(() => {
      TemplateMgr.onHidden(this.pageUrl, this.ndId)
    })
    .onWillAppear(() => {
      TemplateMgr.onWillAppear(this.pageUrl)
    })
    .onWillDisappear(() => {
      TemplateMgr.onWillDisappear(this.pageUrl, this.ndId)
    })
    .onWillShow(() => {
      TemplateMgr.onWillShow(this.pageUrl, this.ndId)
    })
    .onWillHide(() => {
      TemplateMgr.onWillHide(this.pageUrl, this.ndId)
    })
    .onReady((navContext: NavDestinationContext) => {
      this.ndId = navContext.navDestinationId!
      TemplateMgr.updateAnimatorHandle(this.pageUrl, this.ndId)
      TemplateMgr.updateLifecycleNavContext(this.pageUrl, navContext)
      TemplateMgr.onReady(this.pageUrl, this.ndId)
    })
    .onBackPressed(() => {
      return TemplateMgr.onBackPressed(this.pageUrl, this.ndId)
    })
  }
}